package org.example.sensor.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.example.sensor.dox.SensorData;

import org.example.sensor.service.SensorDataHandler;
import org.example.sensor.service.SensorDataService;
import org.example.sensor.vo.ResultVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/sensor-data/")
@RequiredArgsConstructor
public class SensorDataController {
    private final SensorDataHandler dataHandler;
    private final SensorDataService sensorDataService;
    @Operation(summary = "订阅实时数据流")
    @GetMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<SensorData>>  streamData() {
        return dataHandler.getDataStream()
                .map(data -> ServerSentEvent.builder(data).event("message").build())
                .onErrorResume(e -> Flux.just(ServerSentEvent.<SensorData>builder()
                        .event("error")
                        .data(new SensorData())
                        .build()));
    }
    @GetMapping("getAllData")
    public ResultVO getAllData() {
        return ResultVO.success(sensorDataService.findAll());
    }
    @GetMapping("history")
    public ResponseEntity<PageResponse<SensorData>> getHistoryData(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size) {
        System.out.println(start);
        System.out.println(end);
        Page<SensorData> dataPage = sensorDataService.getPagedData(
                start,
                end,
                PageRequest.of(page, size, Sort.by("timestamp").descending())
        );

        return ResponseEntity.ok(
                new PageResponse<>(
                        dataPage.getContent(),
                        dataPage.getNumber(),
                        dataPage.getSize(),
                        dataPage.getTotalElements(),
                        dataPage.getTotalPages()
                )
        );
        }
    // 分页响应DTO
    @Data
    @AllArgsConstructor
    static class PageResponse<T> {
        private List<T> content;
        private int currentPage;
        private int pageSize;
        private long totalItems;
        private int totalPages;
    }

}
